#nullable enable

using System;
using System.ComponentModel;

namespace PHMEE.CommonUtils;

public static class EnumExtensions
{
    public static string GetDescription(this Enum value)
    {
        var type = value.GetType();
        var name = Enum.GetName(type, value);
        if (name == null)
        {
            return string.Empty;
        }

        var field = type.GetField(name);
        if (field == null)
        {
            return string.Empty;
        }

        if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is not DescriptionAttribute attribute)
        {
            return name;
        }

        return attribute.Description;
    }
}